package com.hy.study.practice.hashtable;


import java.util.Scanner;

/**
 * HashTable 的练习
 */
public class HashTaskDemo {
    public static void main(String[] args) {
        HashTable hashTable = new HashTable(10);
        Scanner scanner = new Scanner(System.in);
        String key = "";
        while (true) {
            System.out.println("add: 添加\tlist: 显示\tfind: 查找\tdelete: 删除\texit :退出");
            key = scanner.next();
            switch (key) {
                case "add":
                    System.out.println("请输入ID:");
                    int id = scanner.nextInt();
                    System.out.println("请输入name:");
                    String name = scanner.next();
                    System.out.println("请输入薪水：");
                    double salary = scanner.nextDouble();
                    Emp emp = new Emp(id, name, salary);
                    hashTable.add(emp);
                    break;
                case "list":
                    hashTable.list();
                    break;
                case "find":
                    System.out.println("请输入查询的ID:");
                    id = scanner.nextInt();
                    System.out.println("根据id=" + id + "查询结果:" + hashTable.findById(id));
                    break;
                case "delete":
                    System.out.println("请输入删除的ID:");
                    id = scanner.nextInt();
                    hashTable.delete(id);
                    break;
                case "exit":
                    scanner.close();
                    System.exit(0);
                    break;
                default:
                    break;

            }
        }
    }
}

class HashTable {
    private int maxSize = 16;
    private LinkedList[] arrayLinkedList;

    /**
     * 添加元素
     */
    public void add(Emp emp) {
        int hashCode = this.hashCode(emp.getId());
        arrayLinkedList[hashCode].add(emp);
    }

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    public Emp findById(int id) {
        int hashCode = this.hashCode(id);
        return arrayLinkedList[hashCode].findById(id);
    }

    /**
     * 根据ID删除
     *
     * @param id
     */
    public void delete(int id) {
        int hashCode = this.hashCode(id);
        arrayLinkedList[hashCode].delete(id);
    }

    /**
     * 遍历整个哈希表
     */
    public void list() {
        for (int i = 0; i < this.maxSize; i++) {
            arrayLinkedList[i].list(i + 1);
        }
    }

    /**
     * 得到hasCode 确定位置
     *
     * @param id
     * @return
     */
    private int hashCode(int id) {
        return id % this.maxSize;
    }

    public HashTable(int maxSize) {
        this.maxSize = maxSize;
        arrayLinkedList = new LinkedList[this.maxSize];
        //这里需要初始化数组
        for (int i = 0; i < this.maxSize; i++) {
            arrayLinkedList[i] = new LinkedList();
        }
    }

    public HashTable() {
        arrayLinkedList = new LinkedList[this.maxSize];
        //这里需要初始化数组
        for (int i = 0; i < this.maxSize; i++) {
            arrayLinkedList[i] = new LinkedList();
        }
    }
}

class LinkedList {
    private Emp head;

    /**
     * 向队列中添加元素
     *
     * @param emp
     */
    public void add(Emp emp) {
        if (head == null) {
            head = emp;
            return;
        }
        Emp temp = head;

        while (temp.getNext() != null) {
            temp = temp.getNext();
        }
        temp.setNext(emp);
    }

    /**
     * 显示整个链表中的数据
     */
    public void list(int i) {
        if (head == null) {
            System.out.println("第" + i + "个链表为空");
            return;
        }
        Emp temp = head;
        System.out.printf("第%d个链表元素是:", i);
        while (temp != null) {
            System.out.printf(temp + "-->\t");
            temp = temp.getNext();
        }
        System.out.println();
    }

    /**
     * 通过ID查找
     *
     * @param id
     * @return
     */
    public Emp findById(int id) {
        if (head == null) {
            System.out.println("链表为空");
            return null;
        }
        Emp temp = head;
        boolean flag = false;
        while (temp != null) {
            if (temp.getId() == id) {
                flag = true;
                break;
            }
            temp=temp.getNext();
        }
        if (flag) {
            return temp;
        }
        return null;
    }

    /**
     * 根据指定ID删除元素
     *
     * @param id
     */
    public void delete(int id) {
        if (head == null) {
            System.out.println("列表为空无法删除");
            return;
        }
        if (head.getId() == id) {
            head.setNext(head.getNext());
            return;
        }
        Emp temp = head;
        boolean flag = false;
        while (temp.getNext() != null) {
            if (temp.getNext().getId() == id) {
                flag = true;
                break;
            }
            temp = temp.getNext();
        }
        temp.setNext(temp.getNext().getNext());
    }
}

/**
 * Hash表中存入的元素
 */
class Emp {
    private int id;
    private String name;
    private double salary;
    private Emp next;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public Emp getNext() {
        return next;
    }

    public void setNext(Emp next) {
        this.next = next;
    }

    public Emp(int id, String name, double salary) {
        this.id = id;
        this.name = name;
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", salary=" + salary +
                '}';
    }
}


